package com.project.controller.web;

import com.alibaba.fastjson.JSONObject;
import com.project.entity.Image;
import com.project.entity.OrgInfo;
import com.project.myBeans.ResponseBean;
import com.project.service.ImagesService;
import com.project.service.OrgInfoService;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by WheelChen on 2017/5/15.
 */
@Controller
@RequestMapping("/orginfo")
public class OrginfoController extends BaseService {
    @Autowired
    private OrgInfoService orgInfoService;
    @Autowired
    private ImagesService imagesService;

    /**
     * @api {post} /findByCondition/findByCondition 模糊搜索分页显示公司信息
     * @apiName findByCondition
     * @apiGroup OrgInfo
     * @apiVersion 0.0.1

     * @apiParam {json}  orgInfo 公司信息
     *
     * @apiSuccess {array}  data 返回数据
     * @apiSuccess {int} total  总数
     * @apiSuccess {boolean} success 成功与否 true - 成功 false - 失败
     * @apiSuccess {String} message 提示信息
     *
     *
     * @apiParamExample {json} Request-Example:
     * {
     *     	"pageSize":3,
     *      "pageIndex":1,
     *      "isTemporary":1//是否显示暂存信息 1-不显示 其余表示显示
     * }
     *
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       "data":[
     *       {...},
     *       {...}
     *       ]
     *       "flag": 0,
     *       "message": "查询成功",
     *       "success": true,
     *       "total": 2
     *     }
     *
     *
     *
     * @apiErrorExample Error-Response:
     *     HTTP/1.1 404 程序异常
     *     {
     *       "success": false,
     *       "message":"程序异常"
     *     }
     */
    /**
     * author: WheelChen
     * date: 2017-5-15
     * function: 模糊搜索分页显示
     *
     * @param orgInfo
     * @return
     */
    @RequestMapping(value = "/findByCondition", method = RequestMethod.POST)
    @ResponseBody
    public ResponseBean findByCondition(@RequestBody OrgInfo orgInfo) {
        ResponseBean responseBean = new ResponseBean();
        try {
            List<OrgInfo> orgInfoList;
            orgInfo.setPageIndex(orgInfo.getPageIndex() - 1);
            orgInfoList = orgInfoService.findByCondition(orgInfo);
            responseBean.setData(orgInfoList);
            responseBean.setTotal(orgInfo.getTotal());
            responseBean.setSuccess(true);
            responseBean.setMessage("查询成功");
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("查询失败");
        }
        return responseBean;
    }

    /**
     * author: WheelChen
     * date: 2017-5-21
     * function: 根据managerId查找公司信息
     *
     * @param managerId
     * @return
     */
    @RequestMapping(value = "/findByManagerId/{managerId}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean findByManagerId(@PathVariable("managerId") Integer managerId, HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        try {
            OrgInfo orgInfo = orgInfoService.findByManagerId(managerId);
            if (orgInfo == null) {
                responseBean.setData(null);
                responseBean.setMessage("无对应公司");
            } else {
                Image businessliscense = null;
                Image outsidepic = null;
                JSONObject jsonObject = new JSONObject();
                if (orgInfo.getBusinessliscense() != null) {
                    businessliscense = imagesService.findById(orgInfo.getBusinessliscense());
                    // 解密营业执照
                    String absolutePath = request.getSession().getServletContext().getRealPath(businessliscense.getPath());
                    String base64 = imagesService.dec(absolutePath);//base64编码的图片数据
                    businessliscense.setPath(base64);
                }
                if (orgInfo.getOutsidepic() != null)
                    outsidepic = imagesService.findById(orgInfo.getOutsidepic());
                jsonObject.put("orgInfo", orgInfo);
                jsonObject.put("businessliscense", businessliscense);
                jsonObject.put("outsidepic", outsidepic);
                responseBean.setData(jsonObject);
                responseBean.setSuccess(true);
                responseBean.setMessage("查询成功");
            }
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("查询失败");
        }
        return responseBean;
    }

    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/6/26
     * function: 通过id查找公司信息
     *
     * @param id
     * @return
     */
    public ResponseBean findById(@PathVariable("id") Integer id, HttpServletRequest request) {
        try {
            OrgInfo orgInfo = orgInfoService.findByid(id);
            if (orgInfo == null) {
                responseBean.setMessage("无对应公司");
                responseBean.setSuccess(true);
            } else {
                Image businessliscense = null;
                Image outsidepic = null;
                JSONObject jsonObject = new JSONObject();
                if (orgInfo.getBusinessliscense() != null) {
                    businessliscense = imagesService.findById(orgInfo.getBusinessliscense());
                    // 解密营业执照
                    String absolutePath = request.getSession().getServletContext().getRealPath(businessliscense.getPath());
                    String base64 = imagesService.dec(absolutePath);//base64编码的图片数据
                    businessliscense.setPath(base64);
                }
                if (orgInfo.getOutsidepic() != null)
                    outsidepic = imagesService.findById(orgInfo.getOutsidepic());
                jsonObject.put("orgInfo", orgInfo);
                jsonObject.put("businessliscense", businessliscense);
                jsonObject.put("outsidepic", outsidepic);
                responseBean.setData(jsonObject);
                responseBean.setSuccess(true);
                responseBean.setMessage("查询成功");
            }
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    /**
     * author: WheelChen
     * date: 2017-5-22
     * function: （根据managerId）新建或修改公司信息
     *
     * @param orgInfo 招聘信息
     * @return
     */
    @RequestMapping(value = "/modifyOrAddByManagerId", method = RequestMethod.POST)
    @ResponseBody
    public ResponseBean modifyByManagerId(@RequestBody OrgInfo orgInfo) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (orgInfo.getManagerid() != null) { //先查找改管理员是否有对应公司
                OrgInfo orgInfo1 = orgInfoService.findByManagerId(orgInfo.getManagerid());
                if (orgInfo1 != null) //有则更新 无则插入
                    processSuccessDeleteUpdate(orgInfoService.modifyByManagerId(orgInfo), responseBean);
                else
                    processSuccessDeleteUpdate(orgInfoService.addOrgInfo(orgInfo), responseBean);
            } else {
                processSuccessDeleteUpdate(orgInfoService.addOrgInfo(orgInfo), responseBean);
            }
            return responseBean;
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常，保存失败");
            return responseBean;
        }
    }

    @RequestMapping(value = "/modifyState/{id}/{state}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/6/26
     * function: 根据
     *
     * @param id 公司信息id
     * @param state {int}状态 1- 待审核 2- 审核通过 3- 审核不通过
     * @return
     */
    public ResponseBean modifyState(@PathVariable("id") Integer id, @PathVariable("state") Integer state) {
        ResponseBean responseBean = new ResponseBean();
        try {
            OrgInfo orgInfo = orgInfoService.findByid(id);
            if (orgInfo != null) {
                orgInfo.setState(state);
                processSuccessDeleteUpdate(orgInfoService.modifyById(orgInfo), responseBean);
            } else {
                responseBean.setSuccess(false);
                responseBean.setMessage("公司不存在");
            }
            return responseBean;
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
            return responseBean;
        }
    }

    @RequestMapping(value = "/addOne", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/7/24
     * function: 添加一个新的公司信息
     *
     * @param orgInfo
     * @return
     */
    public ResponseBean addOne(@RequestBody OrgInfo orgInfo) {
        ResponseBean responseBean = new ResponseBean();
        try {
            orgInfoService.addOrgInfo(orgInfo);
            responseBean.setMessage("添加成功");
            responseBean.setSuccess(true);
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/modifyOneById", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/7/24
     * function: 根据id修改公司信息
     *
     * @param orgInfo
     * @return
     */
    public ResponseBean modifyOneById(@RequestBody OrgInfo orgInfo) {
        ResponseBean responseBean = new ResponseBean();
        try {
            processSuccessDeleteUpdate(orgInfoService.modifyById(orgInfo), responseBean);
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean findAll() {
        ResponseBean responseBean = new ResponseBean();
        try {
            List<OrgInfo> orgInfoList = orgInfoService.findAll();
            responseBean.setData(orgInfoList);
            responseBean.setSuccess(true);
            responseBean.setMessage("查找成功");
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }


}
